﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;
using QLTV_V2.DataAccess;

namespace QLTV_V2
{
    public partial class FrmTimKiemBanDoc : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmTimKiemBanDoc()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        ExportToExcel exp = new ExportToExcel();
        string sql;
        DataTable dtb=null;
        private void FrmTimKiemBanDoc_Load(object sender, EventArgs e)
        {
            //Load combobox nhom ban doc
            DataTable tbNhom = uti.getDataSource("NhomBanDoc", "TenNhom");
            comb_MaNhom.DataSource = tbNhom;
            DataRow drLoaiSach = tbNhom.NewRow();
            drLoaiSach["MaNhom"] = "all";
            drLoaiSach["TenNhom"] = "Tất cả";
            tbNhom.Rows.InsertAt(drLoaiSach, 0);
            comb_MaNhom.DisplayMember = "TenNhom";
            comb_MaNhom.ValueMember = "MaNhom";
            comb_MaNhom.SelectedIndex = 0;
            //Load trang thai
            check_Tu.Checked = false;
            check_Den.Checked = false;
            date_Tu.Enabled = false;
            date_Den.Enabled = false;

        }

        private void btn_TimKiem_Click(object sender, EventArgs e)
        {
            dgv_HienThi.DataSource = conn.GetDataTable(getSql());
        }

        private void btn_XemChiTiet_Click(object sender, EventArgs e)
        {
            try
            {
                int dong = dgv_HienThi.CurrentRow.Index;
                FrmLaptheBandoc frm = new FrmLaptheBandoc();
                frm.mabandoc = dgv_HienThi.Rows[dong].Cells["MaBanDoc"].Value.ToString();
                frm.kt = 2;
                frm.ShowDialog();
            }
            catch {
                MessageBox.Show("Không có dữ liệu","Lỗi");
            }
        }
        private void dataGridView_HienThi_DoubleClick(object sender, EventArgs e)
        {
            btn_XemChiTiet_Click(sender, e);
        }
        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        string getSql()
        {
            
            int type = uti.GetTypeDate();
            sql = "select b.MaBanDoc, TenBanDoc,a.MaNhom,TenNhom, NgaySinh, DiaChi,NgayLapThe, NgayHetHan,AnhCaNhan, TTThe " +
                  "from NhomBanDoc a ,BanDoc b " +
                  "where a.MaNhom=b.MaNhom ";
            if (comb_MaNhom.SelectedIndex != 0)
                sql += " and a.MaNhom = N'" + comb_MaNhom.SelectedValue.ToString() + "' ";
            if (txt_MaBanDoc.Text != "")
                sql += "and b.MaBanDoc like N'%" + txt_MaBanDoc.Text + "%' ";
            if (txt_TenBanDoc.Text != "")
                sql += "and TenBanDoc like N'%"+txt_TenBanDoc.Text+"%' ";
            if (txt_DiaChi.Text != "")
                sql += "and DiaChi like N'%"+txt_DiaChi.Text+"%' ";
            //Ngày tháng
            if (check_Tu.Checked == true && check_Den.Checked == false)
                sql += "and NgayLapThe>='"+date_Tu.Value.ToString("yyyy-MM-dd")+"' ";
            if (check_Tu.Checked == false && check_Den.Checked == true)
                sql += "and NgayLapThe <='" + date_Den.Value.ToString("yyyy-MM-dd") + "' ";
            if (check_Tu.Checked == true && check_Den.Checked == true)
                sql += "and NgayLapThe BETWEEN '" + date_Tu.Value.ToString("yyyy-MM-dd") + "' and '" + date_Den.Value.ToString("yyyy-MM-dd") + "'";
            //TTThe
            if (check_HetHan.Checked == true)
                sql += "and NgayHetHan<getdate()";
            if(check_GiaHan.Checked==true)
                sql+="and TTThe =N'Kích hoạt' ";
            if(check_Khoa.Checked==true)
                sql+="and TTThe=N'Bị khóa' ";
            return sql;
        }

        private void check_Tu_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Tu.Checked == true)
                date_Tu.Enabled = true;
            else
                date_Tu.Enabled = false;
        }

        private void check_Den_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Den.Checked == true)
                date_Den.Enabled = true;
            else
                date_Den.Enabled = false;
        }

        private void dataGridView_HienThi_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_HienThi);
        }

        private void btn_ExportExcel_Click(object sender, EventArgs e)
        {

            exp.exportExcel_2(dgv_HienThi,dtb,1);
        }


    }
}
